﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;

namespace Logistics.Configuration
{
    public class GlobalSetting
    {
        #region 获取AppSetting信息
        public static string GetSoftWareBase(string name)
        {
            return ConfigurationManager.AppSettings[name];
        }
        /// <summary>
        /// 配置的当前版本号
        /// </summary>
        public static String CurrentVersion
        {
            get { return ConfigurationManager.AppSettings["current-version"]; }
        }
        /// <summary>
        /// 软件更新源
        /// </summary>
        public static String UpdateSourceURL
        {
            get { return ConfigurationManager.AppSettings["update-url"]; }
        }
        #endregion

        private String configFilePath = "";
        private XmlFiles m_XDoc = null;
        public GlobalSetting(String xmlPath)
        {
            this.configFilePath = xmlPath;
            m_XDoc = new XmlFiles(xmlPath);
        }

        public String MachineID
        {
            get 
            { 
                String ID = XmlFiles.Read(configFilePath, "/system/machine/ID", "");
                return ID;
            }
            set
            {
                XmlFiles.Update(configFilePath, "/system/machine/ID", "", value);
            }
        }
        public String MachineSiteId
        {
            get
            {
                String name = XmlFiles.Read(configFilePath, "/system/machine/site", "id");
                return name;
            }
            set
            {
                XmlFiles.Update(configFilePath, "/system/machine/site", "id", value);
            }
        }
        public String MachineSiteName
        {
            get
            {
                String name = XmlFiles.Read(configFilePath, "/system/machine/site", "");
                return name;
            }
            set
            {
                XmlFiles.Update(configFilePath, "/system/machine/site", "", value);
            }
        }
        public String MachineOutletId
        {
            get
            {
                String name = XmlFiles.Read(configFilePath, "/system/machine/outlet", "id");
                return name;
            }
            set
            {
                XmlFiles.Update(configFilePath, "/system/machine/outlet", "id", value);
            }
        }
        public String MachineOutletName
        {
            get
            {
                String name = XmlFiles.Read(configFilePath, "/system/machine/outlet", "");
                return name;
            }
            set
            {
                XmlFiles.Update(configFilePath, "/system/machine/outlet", "", value);
            }
        }
        public String WebServiceEndPoint
        {
            get
            {
                String addr = XmlFiles.Read(configFilePath, "/system/URL", "");
                return addr;
            }
        }
        public String LastDownloadTime
        {
            get
            {
                String addr = XmlFiles.Read(configFilePath, "/system/downCustomTime", "");
                return addr;
            }
            set 
            {
                XmlFiles.Update(configFilePath, "/system/downCustomTime", "", value);
            }
        }

        public String[] DownloadTables
        {
            get 
            {
                List<String> tabNames = new List<String>();
                XmlNodeList list = m_XDoc.SelectNodes("/system/downloadTables/table/@TableName");
                if (list != null)
                {
                    foreach (XmlNode node in list)
                        tabNames.Add(node.Value);
                }
                return tabNames.ToArray();
            }
        }

        /// <summary>
        /// 发货人标志
        /// </summary>
        public String ConsignorFlags
        {
            get
            {
                String addr = XmlFiles.Read(configFilePath, "/system/col/consignor", "");
                return addr;
            }
        }
        
    }
}
